package com.util.core;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.web.servlet.ModelAndView;

public abstract class AbstractBusiness implements Business {
	
	protected Map<String,?> model;
	
	public Map<?,?> getModel() {
		return model;
	}

	public void setModel(Map<String,?> model) {
		this.model = model;
	}

	public ModelAndView businessRun(){
		return this.businessRequest();
	}
	
	public ModelAndView businessRun(String methodName){
		return methodRun(methodName);
	}
	
	protected ModelAndView businessRequest(){
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public ModelAndView methodRun(String methodName){
		Class<? extends AbstractBusiness> cls = this.getClass();
		Method method = null;
		try {
			method = cls.getMethod(methodName, new Class[]{});
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		Map<String,?> model = null;
		try {
			model = (Map<String,?>)method.invoke(this,new Object[]{});
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return new ModelAndView("model",model);
	}
}
